package com.zhaojk.audio.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Collections;

/**
 * @author zhaojk
 */
@Configuration
public class VolcPicConfig {
    @Bean
    public RateLimiter rateLimiter(){
        return  RateLimiter.create(1.8);
    }

    /**
     * 初始化限流规则
     */
    @PostConstruct
    public void initFlowRules() {
        FlowRule rule = new FlowRule();
        rule.setResource("volcPicResource");  // 资源名称
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // QPS限流
        rule.setCount(2); // 阈值=2请求/秒
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); // 滑动窗口
        rule.setClusterMode(false); // 单机模式

        FlowRuleManager.loadRules(Collections.singletonList(rule));
    }
}
